﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace SangsTools
{
    public static class GUI_Tools
    {
        public static void GUI_DrawLine_World(Vector3 startPos, Vector3 Pos,float lineWidth,Texture texture)
        {
            Vector3 startOnScreen = Camera.main.WorldToScreenPoint(startPos);
            Vector3 endOnScreen = Camera.main.WorldToScreenPoint(Pos);
            //Debug.Log($"startPos:{startPos}  startOnScreen:{startOnScreen}");
            //Debug.Log($"endPos:{Pos}  endOnScreen:{endOnScreen}");

            GUI_DrawLine_Screen(startOnScreen, endOnScreen, lineWidth, texture);
        }

        public static void GUI_DrawLine_Screen(Vector3 startOnScreen, Vector3 endOnScreen, float lineWidth, Texture texture)
        {
            //坐标系不一样，screen 是左下到右上，gui是左上到右下
            startOnScreen.y = UnityEngine.Screen.height - startOnScreen.y;
            endOnScreen.y = UnityEngine.Screen.height - endOnScreen.y;

            //起点是图片左上角，减半个边长，改到左边中间
            Rect rect = new Rect(startOnScreen + Vector3.down * lineWidth * 0.5f,
                new Vector2((startOnScreen - endOnScreen).magnitude, lineWidth));

            Vector3 dir = endOnScreen - startOnScreen;
            float angle = Vector3.Angle(dir, Vector3.right);
            angle = Vector3.Dot(dir, Vector3.up) > 0 ? angle : -angle;

            GUIUtility.RotateAroundPivot(angle, startOnScreen);
            GUI.DrawTexture(rect, texture);
            GUIUtility.RotateAroundPivot(-angle, startOnScreen);
        }
    }
}
